package com.xudow.app.ui;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.vo.RemainedHourVO;
import com.xudow.app.R;
import com.xudow.app.ui.task.RemainClassTask;
import com.xudow.app.util.Maps;
import com.xudow.app.util.StringUtils;

import java.util.HashMap;

import static com.xudow.app.util.LogUtils.makeLogTag;

/**
 * 课程中心Activity
 *
 * @author lukecheng
 */
public class ClazzCourseActivity extends BaseActivity {
    private static final String TAG = makeLogTag(ClazzCourseActivity.class);

    private String studentId;

    private ScrollView scrollView;
    private LinearLayout dataLayout;
    private RelativeLayout emptyLayout;

    private LayoutInflater layoutInflater;

    private RemainClassTask remainClassTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clazz_course);

        studentId = getIntent().getStringExtra("studentId");

        scrollView = (ScrollView) findViewById(R.id.scroll_view);
        dataLayout = (LinearLayout) findViewById(R.id.data_layout);
        emptyLayout = (RelativeLayout) findViewById(R.id.enroll_require_prompt);

        layoutInflater = LayoutInflater.from(this);

        if (studentId != null) {
            showLodingDialog(getString(R.string.loading));

            HashMap<String, String> params = Maps.newHashMap();
            params.put("studentid", studentId);

            remainClassTask = new RemainClassTask(this, dataHandler);
            addTask(remainClassTask);
            remainClassTask.execute(params);
        } else {
            initEmptyView();
        }
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    private Handler dataHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();

            if (RemainClassTask.SUCCESS == msg.what) {
                onLoadSuccess(msg.getData());
            } else {
                String message = getString(R.string.loading_fail);
                Toast.makeText(ClazzCourseActivity.this, message, Toast.LENGTH_LONG).show();
            }
        }
    };

    private void onLoadSuccess(Bundle data) {
        RemainedHourVO[] hoursArr = (RemainedHourVO[]) data.getSerializable("remainHours");

        if (null == hoursArr || hoursArr.length <= 0) {
            initEmptyView();
            return;
        }

        dataLayout.removeAllViews();

        for (RemainedHourVO vo : hoursArr) {

            View view = layoutInflater.inflate(R.layout.item_remain_hour, null);

            String all = StringUtils.isEmpty(vo.getAll()) ? "0" : vo.getAll();
            String surplus = StringUtils.isEmpty(vo.getSurplus()) ? "0" : vo.getSurplus();

            float usedHours = Float.parseFloat(all) - Float.parseFloat(surplus);  //使用了的课时

            ((TextView) view.findViewById(R.id.name)).setText(vo.getClassName());
            ((TextView) view.findViewById(R.id.elapsed_hour)).setText(String.format("%.1f", usedHours));
            ((TextView) view.findViewById(R.id.remain_hour)).setText(String.format("%.1f", Float.parseFloat(surplus)));

            RelativeLayout chart = (RelativeLayout) view.findViewById(R.id.chart);
            TextView chartText = (TextView) view.findViewById(R.id.chart_text);

            setCourseChart(chart, Float.parseFloat(all), usedHours);
            chartText.setText("" + (int) Float.parseFloat(all));

            dataLayout.addView(view);
        }

        scrollView.setVisibility(View.VISIBLE);
        emptyLayout.setVisibility(View.GONE);
    }

    private void setCourseChart(RelativeLayout view, float total, float elapsed) {
        int width = 72;
        int height = 72;

        int remain = (int) (((total - elapsed) / total) * 360);
        int elapse = 360 - remain;

        Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(getResources().getColor(R.color.dark_rvcode));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);

        canvas.drawCircle(width / 2, height / 2, height / 2 - 3, paint);

        Paint paintElapsed = new Paint();
        paintElapsed.setAntiAlias(true);
        paintElapsed.setColor(getResources().getColor(R.color.light_rvcode));
        paintElapsed.setStyle(Paint.Style.STROKE);
        paintElapsed.setStrokeWidth(10);

        canvas.drawArc(new RectF(9, 9, width - 9, height - 9), 270 + remain, elapse, false, paintElapsed);

        Paint paintRemain = new Paint();
        paintRemain.setAntiAlias(true);
        paintRemain.setColor(getResources().getColor(R.color.rvcode));
        paintRemain.setStyle(Paint.Style.STROKE);
        paintRemain.setStrokeWidth(10);

        canvas.drawArc(new RectF(9, 9, width - 9, height - 9), 270, remain, false, paintRemain);

        Drawable drawable = new BitmapDrawable(getResources(), bitmap);
        view.setBackgroundDrawable(drawable);
    }

    private void initEmptyView() {
        emptyLayout.setVisibility(View.VISIBLE);
    }

}
